package com.wondersgroup.wuhan_demo.util;

import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;

import java.io.ByteArrayInputStream;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

public class XMLBuilder {
	private final static DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
	public static Document getDocument(String xml)throws Exception{
		Document returnValue=null;
		DocumentBuilder builder=null;
		ByteArrayInputStream bis=null;
		try{
			bis=new ByteArrayInputStream(xml.getBytes("utf-8"));
			builder=factory.newDocumentBuilder();
			returnValue=builder.parse(bis);
		}finally{
			if(bis!=null){
				try{
					bis.close();
				}catch(Exception ne){
				}
				bis=null;
			}
		}
		return returnValue;
	}
	public static String getArribute(Node node,String attribute){
		String returnValue=null;
		Node item=null;
		NamedNodeMap namedNodeMap=null;
		if(node==null){
			return returnValue;
		}
		namedNodeMap=node.getAttributes();
		if(namedNodeMap==null){
			return returnValue;
		}
		item=namedNodeMap.getNamedItem(attribute);
		if(item==null){
			return returnValue;
		}
		returnValue=item.getNodeValue();
		return returnValue;
	}
}
